<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>Linux 命令行参数处理函数探究(转)  &middot; 程序猿的小本本</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">


<meta name="description" content="光说有毛用|程序猿的小本本" />

<meta name="keywords" content="cmdline, getopt, getopt_long, linux, ">


<meta property="og:title" content="Linux 命令行参数处理函数探究(转)  &middot; 程序猿的小本本 ">
<meta property="og:site_name" content="程序猿的小本本"/>
<meta property="og:url" content="http://replace-this-with-your-hugo-site.com/2015/08/23/linux-getopt.html" />
<meta property="og:locale" content="en-us">


<meta property="og:type" content="article" />
<meta property="og:description" content=""/>
<meta property="og:article:published_time" content="2015-08-23T01:59:32Z" />
<meta property="og:article:modified_time" content="2015-08-23T01:59:32Z" />

  
    
<meta property="og:article:tag" content="cmdline">
    
<meta property="og:article:tag" content="getopt">
    
<meta property="og:article:tag" content="getopt_long">
    
<meta property="og:article:tag" content="linux">
    
  

  
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="@" />
<meta name="twitter:creator" content="@" />
<meta name="twitter:title" content="Linux 命令行参数处理函数探究(转)" />
<meta name="twitter:description" content="" />
<meta name="twitter:url" content="http://replace-this-with-your-hugo-site.com/2015/08/23/linux-getopt.html" />
<meta name="twitter:domain" content="http://replace-this-with-your-hugo-site.com/">
  

<script type="application/ld+json">
  {
    "@context": "http://schema.org",
    "@type": "Article",
    "headline": "Linux 命令行参数处理函数探究(转)",
    "author": {
      "@type": "Person",
      "name": "http://profiles.google.com/+?rel=author"
    },
    "datePublished": "2015-08-23",
    "description": "",
    "wordCount":  793 
  }
</script>



<link rel="canonical" href="http://replace-this-with-your-hugo-site.com/2015/08/23/linux-getopt.html" />

<link rel="apple-touch-icon-precomposed" sizes="144x144" href="http://replace-this-with-your-hugo-site.com/touch-icon-144-precomposed.png">
<link href="http://replace-this-with-your-hugo-site.com/favicon.png" rel="icon">
<meta name="theme-color" content="#2053AB">
<meta name="generator" content="Hugo 0.17-DEV" />

  <!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

<link href='https://fonts.googleapis.com/css?family=Merriweather:300%7CRaleway%7COpen+Sans' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="http://replace-this-with-your-hugo-site.com/css/font-awesome.min.css">
<link rel="stylesheet" href="http://replace-this-with-your-hugo-site.com/css/style.css">
<link rel="stylesheet" href="http://replace-this-with-your-hugo-site.com/css/highlight/default.css">

  
</head>
<body>
  <main id="main-wrapper" class="container main_wrapper has-sidebar">
    <header id="main-header" class="container main_header">
  <div class="container brand">
  <div class="container title h1-like">
  <a class="baselink" href="http://replace-this-with-your-hugo-site.com/">
  程序猿的小本本

</a>

</div>

  
<div class="container topline">
  
  光说有毛用


</div>


</div>

  <nav class="container nav primary no-print">
  

<a class="homelink" href="http://replace-this-with-your-hugo-site.com/">Home</a>


  
<a href="http://replace-this-with-your-hugo-site.com/about">About</a>

<a href="http://replace-this-with-your-hugo-site.com/post" title="Show list of posts">Posts</a>

<a href="http://replace-this-with-your-hugo-site.com/tags" title="Show list of tags">Tags</a>


</nav>

<div class="container nav secondary no-print">
  
<a id="contact-link-email" class="contact_link" href="mailto:chennqqi@qq.com">
  <span class="fa fa-envelope-square"></span><span>email</span></a>



<a id="contact-link-github" class="contact_link" href="https://github.com/chennqqi?tab=repositories">
  <span class="fa fa-github-square"></span><span>github</span></a>

















</div>


  

</header>


<article id="main-content" class="container main_content single">
  <header class="container hat">
  <h1>Linux 命令行参数处理函数探究(转)
</h1>

  <div class="metas">
<time datetime="2015-08-23">23 Aug, 2015</time>


  
    &middot; by admin
  
  &middot; Read in about 4 min
  &middot; (793 Words)
  <br>
  
<a class="label" href="http://replace-this-with-your-hugo-site.com/tags/cmdline">cmdline</a>

<a class="label" href="http://replace-this-with-your-hugo-site.com/tags/getopt">getopt</a>

<a class="label" href="http://replace-this-with-your-hugo-site.com/tags/getopt_long">getopt_long</a>

<a class="label" href="http://replace-this-with-your-hugo-site.com/tags/linux">linux</a>



</div>

</header>

  <div class="container content">
  <p>一、getopt函数</p>

<p><img src="http://blog.chinaunix.net/attachment/201205/20/26833883_1337526381qRMV.png" alt="" /></p>

<div>
  A.函数功能：处理命令行参数
</div>

<div>
</div>

<div>
  B.参数:
</div>

<div>
</div>

<div>
  argc代表命令行参数的个数，argv[i]存放命令行参数在内存存放的地址。这两个参数直接从mian(int argc,char *argv[])得到。
</div>

<div>
</div>

<div>
  如果argv的元素是以 &#8220;-“开头称为一个选项元素,紧跟在&#8221;-&#8220;后面的一个字符称为选项字符。
</div>

<div>
</div>

<div>
  eg:ls  -l
</div>

<div>
</div>

<div>
  其中ls是可执行程序名称，-l称为选项元素，&#8217;l&#8217;称为选项字符
</div>

<div>
</div>

<div>
  optstring:选项参数的集合
</div>

<div>
</div>

<div>
  注意:如果getopt()函数被循环调用，每次调用都会返回一个对应选元素的选项字符,如果所有选项字符都放回后，再次调用getopt()函数返回-1。
</div>

<div>
</div>

<div>
</div>

<div>
  <pre class="lang:c decode:true ">./a.out   -a  -n  -l
 
while(1)
{
    ch = getopt(argc,argv,"anl");
}</pre>
  
  <p>
    &nbsp;
  </p>
</div>

<div>
  第一次调用ch : &#8216;a&#8217;
</div>

<div>
  第二次调用ch : &#8216;n&#8217;
</div>

<div>
  第三次调用ch : &#8216;l&#8217;
</div>

<div>
</div>

<div>
  <p>
    案例一、
  </p>
</div>

<p><a class="evt" href="http://blog.chinaunix.net/attachment/201205/20/26833883_1337526418yiyG.png" target="_blank"><img src="http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&o=1&et=normal&m=2_0%3ADzzzzye7hPs%24----3XZGQex&f=1f4be180-7e44-4e3f-9802-b2e7872c9dc2&n=26833883_1337526418yiyG.png&ext=png" alt="" /></a></p>

<p>运行结果：</p>

<p><a class="evt" href="http://blog.chinaunix.net/attachment/201205/20/26833883_1337526453JOOB.png" target="_blank"><img src="http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&o=1&et=normal&m=2_0%3ADzzzzye7hPs%24----3XZGQex&f=c9a8f641-7970-457e-8640-76c5eef14900&n=26833883_1337526453JOOB.png&ext=png" alt="" /></a></p>

<div>
  C.变量optind
</div>

<div>
</div>

<div>
  每次getopt函数调用的时候，optind总是当前处理argv[index]的下一个被处理选项元素的索引即optind = index + 1，如:当前处理的选项元素argv[1]，则optind的值为2,即argv[2]的索引。
</div>

<div>
</div>

<div>
  注意：其值默认初始化为1,如果。
</div>

<div>
</div>

<div>
  <p>
    案例二、
  </p>
  
  <div>
    <ol>
      <li>
        <pre class="lang:c decode:true ">#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
int main(int argc,char *argv[])
{
    int ch;
    while((ch = getopt(argc,argv,"cyg")) != -1)
    {
        switch(ch)
        {
        case 'c':
            printf("optind = %d\n",optind);
            printf("Option character \'c\'.\n");
            break;
        case 'y':
            printf("optind = %d\n",optind);
            printf("Option character \'y\'.\n");
            break;
        case 'g':
            printf("optind = %d\n",optind);
            printf("Option character \'g\'.\n");
            break;
        }
    }
    printf("Ending...\n");
    printf("optind = %d\n",optind);
    return 0;
}</pre>
        
        <p>
          &nbsp;</li> </ol> </div> 
          

<pre><code>      &lt;p&gt;
        运行结果:
      &lt;/p&gt;

      &lt;p&gt;
        &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_1337526516f9w5.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=e78c330d-84fb-4f42-9e4f-5799ac7a0a7d&amp;n=26833883_1337526516f9w5.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
      &lt;/p&gt;

      &lt;div&gt;
        D.变量optarg
      &lt;/div&gt;

      &lt;div&gt;
      &lt;/div&gt;

      &lt;div&gt;
        如果optstring选项字符后面紧跟一个&amp;#8221;:&amp;#8221;，则要求选项元素后面跟一个参数;
      &lt;/div&gt;

      &lt;div&gt;
        如果optstring选项字符后面紧跟两个&amp;#8221;::&amp;#8221;，则选项元素后面可以跟一个可选的参数
      &lt;/div&gt;

      &lt;div&gt;
      &lt;/div&gt;

      &lt;div&gt;
        &lt;p&gt;
          案例三:
        &lt;/p&gt;

        &lt;div&gt;
          &lt;ol&gt;
            &lt;li&gt;
              &lt;pre class=&quot;lang:c decode:true &quot;&gt;#include &amp;lt;stdio.h&amp;gt;
</code></pre>

<p>#include &lt;unistd.h&gt;
int main(int argc,char *argv[])
{
    int ch;
    while((ch = getopt(argc,argv,&laquo;c:yg::&laquo;)) != -1)
    {
        switch(ch)
        {
        case &lsquo;c&rsquo;:
            printf(&laquo;optind = %d\n&raquo;,optind);
            printf(&laquo;optarg = %s\n&raquo;,optarg);
            printf(&laquo;Option character \&lsquo;c\&lsquo;.\n&raquo;);
            break;
        case &lsquo;y&rsquo;:
            printf(&laquo;optind = %d\n&raquo;,optind);
            printf(&laquo;Option character \&lsquo;y\&lsquo;.\n&raquo;);
            break;
        case &lsquo;g&rsquo;:
            printf(&laquo;optind = %d\n&raquo;,optind);
            printf(&laquo;optarg = %s\n&raquo;,optarg);
            printf(&laquo;Option character \&lsquo;g\&lsquo;.\n&raquo;);
            break;
        }
    }
    printf(&laquo;Ending&hellip;\n&raquo;);
    printf(&laquo;argv[%d] = %s\n&raquo;,optind,argv[optind]);
    return 0;
}</pre></p>

<pre><code>              &lt;p&gt;
                &amp;nbsp;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; 

                &lt;p&gt;
                  &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_1337526570jxAE.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=476757e3-8aa2-4d6b-b9f1-7c40b78d1225&amp;n=26833883_1337526570jxAE.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
                &lt;/p&gt;

                &lt;div&gt;
                  注意：上面的最后一次运行结果，&amp;#8221;xxx&amp;#8221;在一开始在argv的的索引值为3,但是我们发现它实际的索引值是4。这是因为getopt函数，会把不拥有选项参数的选项字符后面的操作数，放在最后。
                &lt;/div&gt;

                &lt;div&gt;
                &lt;/div&gt;

                &lt;div&gt;
                  E.变量opterr和变量optopt
                &lt;/div&gt;

                &lt;div&gt;
                &lt;/div&gt;

                &lt;div&gt;
                  getopt()函数在搜索argv时，如果发现argv拥有optstring所没有的选项字符，则会提示立即返回&amp;#8217;?&amp;#8217;,并提示错误信息，所没有的选项字符会放在opt里。如果不想系统提示默认的错误信息，将opterr设为0即可。
                &lt;/div&gt;

                &lt;div&gt;
                &lt;/div&gt;

                &lt;div&gt;
                  &lt;p&gt;
                    案例四:
                  &lt;/p&gt;

                  &lt;div&gt;
                    &lt;ol&gt;
                      &lt;li&gt;
                        &lt;pre class=&quot;lang:c decode:true &quot;&gt;#include &amp;lt;stdio.h&amp;gt;
</code></pre>

<p>#include &lt;unistd.h&gt;
int main(int argc,char *argv[])
{
    int ch;
    while((ch = getopt(argc,argv,&laquo;c:yg::&laquo;)) != -1)
    {
        switch(ch)
        {
        case &lsquo;c&rsquo;:
            printf(&laquo;optind = %d\n&raquo;,optind);
            printf(&laquo;optarg = %s\n&raquo;,optarg);
            printf(&laquo;Option character \&lsquo;c\&lsquo;.\n&raquo;);
            break;
        case &lsquo;y&rsquo;:
            printf(&laquo;optind = %d\n&raquo;,optind);
            printf(&laquo;Option character \&lsquo;y\&lsquo;.\n&raquo;);
            break;
        case &lsquo;g&rsquo;:
            printf(&laquo;optind = %d\n&raquo;,optind);
            printf(&laquo;optarg = %s\n&raquo;,optarg);
            printf(&laquo;Option character \&lsquo;g\&lsquo;.\n&raquo;);
            break;
        case &lsquo;?&rsquo;:
            printf(&laquo;Invalid Option character \&lsquo;%c\&lsquo;.\n&raquo;,optopt);
            break;
        }
    }
    printf(&laquo;Ending&hellip;\n&raquo;);
    printf(&laquo;argv[%d] = %s\n&raquo;,optind,argv[optind]);
    return 0;
}</pre></p>

<pre><code>                        &lt;p&gt;
                          &amp;nbsp;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; 

                          &lt;p&gt;
                            运行结果：
                          &lt;/p&gt;

                          &lt;p&gt;
                            &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_1337526663lAlS.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=969fecda-458d-419b-b5b6-418ea7b2f6c9&amp;n=26833883_1337526663lAlS.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
                          &lt;/p&gt;&lt;/div&gt; 

                          &lt;div&gt;
                            注意:如果optstring字符串一开始就是&amp;#8221;:&amp;#8221;，则后面要求有选项参数的选项字符，在argv中没有选项参数时，此时调用getopt函数返回&amp;#8221;:&amp;#8221;,optopt存放的是没有跟选项参数的选项字符。
                          &lt;/div&gt;

                          &lt;div&gt;
                          &lt;/div&gt;

                          &lt;div&gt;
                            &lt;p&gt;
                              案例五:
                            &lt;/p&gt;

                            &lt;div&gt;
                              &lt;ol&gt;
                                &lt;li&gt;
                                  &lt;pre class=&quot;lang:c decode:true &quot;&gt;#include &amp;lt;stdio.h&amp;gt;
</code></pre>

<p>#include &lt;unistd.h&gt;
int main(int argc,char *argv[])
{
    int ch;
    while((ch = getopt(argc,argv,&raquo;:c:yg::&laquo;)) != -1)
    {
        switch(ch)
        {
        case &lsquo;c&rsquo;:
            printf(&laquo;optind = %d\n&raquo;,optind);
            printf(&laquo;optarg = %s\n&raquo;,optarg);
            printf(&laquo;Option character \&lsquo;c\&lsquo;.\n&raquo;);
            break;
        case &lsquo;y&rsquo;:
            printf(&laquo;optind = %d\n&raquo;,optind);
            printf(&laquo;Option character \&lsquo;y\&lsquo;.\n&raquo;);
            break;
        case &lsquo;g&rsquo;:
            printf(&laquo;optarg = %s\n&raquo;,optarg);
            printf(&laquo;optind = %d\n&raquo;,optind);
            printf(&laquo;Option character \&lsquo;g\&lsquo;.\n&raquo;);
            break;
        case &lsquo;:&rsquo;:
            printf(&laquo;Option character \&lsquo;%c\&lsquo;,forget argument.\n&raquo;,optopt);
            break;
        case &lsquo;?&rsquo;:
            printf(&laquo;optind = %d\n&raquo;,optind);
            printf(&laquo;Invalid Option character \&lsquo;%c\&lsquo;.\n&raquo;,optopt);
            break;
        }
    }
    printf(&laquo;Ending&hellip;\n&raquo;);
    printf(&laquo;argv[%d] = %s\n&raquo;,optind,argv[optind]);
    return 0;
}</pre></p>

<pre><code>                                  &lt;p&gt;
                                    &amp;nbsp;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; 

                                    &lt;p&gt;
                                      运行结果:
                                    &lt;/p&gt;

                                    &lt;p&gt;
                                      &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_1337526728MXSY.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=1ea510f3-b09d-4baf-a471-6ef043040479&amp;n=26833883_1337526728MXSY.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
                                    &lt;/p&gt;

                                    &lt;div&gt;
                                      二、getopt_long函数
                                    &lt;/div&gt;

                                    &lt;div&gt;
                                    &lt;/div&gt;

                                    &lt;div&gt;
                                       getopt_long函数也是处理命令行参数,我们会发现它多了一个long,这个long的意思是它支持长选项。
                                    &lt;/div&gt;

                                    &lt;div&gt;
                                      前面我们讨论都是含有一个&amp;#8221;-&amp;#8220;的选项，常我们会看到&amp;#8221;&amp;#8211;help&amp;#8221;这样的造型，其实这就是命令行参数中的长选项。一般短选项是选项名的缩写,长选项是选项名的全名。&lt;br /&gt; &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_1337526761a12B.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=0250a467-60da-482d-8a3a-8c8d40b85c22&amp;n=26833883_1337526761a12B.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;br /&gt; &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_1337526779136u.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=9ee72e9f-4d2b-4f82-9568-6c97ed9d5f9b&amp;n=26833883_1337526779136u.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt; 

                                      &lt;div&gt;
                                        这个函数前三个参数和getopt一样，不解释了。我们来看看后面两个参数。
                                      &lt;/div&gt;

                                      &lt;div&gt;
                                        getopt_long的长选项是一个结构体，定义如下:
                                      &lt;/div&gt;

                                      &lt;p&gt;
                                        &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_1337526826Y0Ny.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=4788c644-deda-402c-9d54-c8b50537979d&amp;n=26833883_1337526826Y0Ny.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;br /&gt; &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_1337526848iHkr.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=bddbf760-90e4-4313-b9c4-f8749ad19fbe&amp;n=26833883_1337526848iHkr.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
                                      &lt;/p&gt;

                                      &lt;div&gt;
                                        参数longindex不为空时，存放的是getopt_long处理当前长选项的索引值。
                                      &lt;/div&gt;

                                      &lt;div&gt;
                                      &lt;/div&gt;

                                      &lt;div&gt;
                                        &lt;p&gt;
                                          案例六:
                                        &lt;/p&gt;

                                        &lt;div&gt;
                                          &lt;ol&gt;
                                            &lt;li&gt;
                                              &lt;pre class=&quot;lang:c decode:true &quot;&gt;#include &amp;lt;stdio.h&amp;gt;
</code></pre>

<p>#include &lt;unistd.h&gt;
#include &lt;getopt.h&gt;
int main(int argc,char *argv[])
{
    int flag_value = 100;
    while(1)
    {
        int option_index = 0;
        int rvalue = 0;
        static struct option long_option[] = {
            {&laquo;help0&raquo;,no_argument,0,0},
            {&laquo;help1&raquo;,required_argument,0,0},
            {&laquo;help2&raquo;,optional_argument,0,0},
            {&laquo;help3&raquo;,no_argument,0,10},
            {0,0,0,0},
        };
        long_option[3].flag = &amp;flag_value;
        rvalue = getopt_long(argc,argv,&laquo;a:bc::&laquo;,long_option,&amp;option_index);
        if(rvalue == -1)
        {
            printf(&laquo;No more argument.\n&raquo;);
            return -1;
        }
        switch(option_index)
        {
        case 0:
            printf(&laquo;Long option is : %s\n&raquo;,long_option[option_index].name);
            break;
        case 1:
            printf(&laquo;Long option is : %s &laquo;,long_option[option_index].name);
            if(optarg)
            {
                printf(&laquo;with parm &lsquo;%s&rsquo;&raquo;,optarg);
            }
            printf(&raquo;\n&raquo;);
            break;
        case 2:
            printf(&laquo;Long option is : %s &laquo;,long_option[option_index].name);
            if(optarg)
            {
                printf(&laquo;with parm &lsquo;%s&rsquo;&raquo;,optarg);
            }
            printf(&raquo;\n&raquo;);
            break;
        case 3:
            printf(&laquo;Long option is : %s\n&raquo;,long_option[option_index].name);
            break;
        }
        printf(&laquo;flag_value = %d\n&raquo;,flag_value);
    }
    return 0;
}</pre></p>

<pre><code>                                              &lt;p&gt;
                                                &amp;nbsp;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; 

                                                &lt;p&gt;
                                                  运行结果：
                                                &lt;/p&gt;

                                                &lt;p&gt;
                                                  &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_1337526922IiY2.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=eeed00f5-f0fe-4441-8cae-196af0abe73d&amp;n=26833883_1337526922IiY2.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
                                                &lt;/p&gt;

                                                &lt;p&gt;
                                                  三、getopt_long_only函数
                                                &lt;/p&gt;

                                                &lt;p&gt;
                                                  &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_13375269502qqp.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=aa5692ae-dfa9-4ffb-9feb-d74c50616a73&amp;n=26833883_13375269502qqp.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;br /&gt; &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_1337526976l7vv.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=577129ce-6c34-4f6b-b3b9-e960116757f5&amp;n=26833883_1337526976l7vv.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
                                                &lt;/p&gt;

                                                &lt;p&gt;
                                                  案例:
                                                &lt;/p&gt;

                                                &lt;div&gt;
                                                  &lt;ol&gt;
                                                    &lt;li&gt;
                                                      &lt;pre class=&quot;lang:c decode:true&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
</code></pre>

<p>#include &lt;unistd.h&gt;
#include &lt;getopt.h&gt;
int main(int argc,char *argv[])
{
    int flag_value = 100;
    while(1)
    {
        int option_index = 0;
        int rvalue = 0;
        static struct option long_option[] = {
            {&laquo;help0&raquo;,no_argument,0,0},
            {&laquo;help1&raquo;,required_argument,0,0},
            {&laquo;help2&raquo;,optional_argument,0,0},
            {&laquo;help3&raquo;,no_argument,0,10},
            {0,0,0,0},
        };
        long_option[3].flag = &amp;flag_value;
        rvalue = getopt_long_only(argc,argv,&laquo;a:bc::&laquo;,long_option,&amp;option_index);
        if(rvalue == -1)
        {
            printf(&laquo;No more argument.\n&raquo;);
            return -1;
        }
        switch(option_index)
        {
        case 0:
            printf(&laquo;Long option is : %s\n&raquo;,long_option[option_index].name);
            break;
        case 1:
            printf(&laquo;Long option is : %s &laquo;,long_option[option_index].name);
            if(optarg)
            {
                printf(&laquo;with parm &lsquo;%s&rsquo;&raquo;,optarg);
            }
            printf(&raquo;\n&raquo;);
            break;
        case 2:
            printf(&laquo;Long option is : %s &laquo;,long_option[option_index].name);
            if(optarg)
            {
                printf(&laquo;with parm &lsquo;%s&rsquo;&raquo;,optarg);
            }
            printf(&raquo;\n&raquo;);
            break;
        case 3:
            printf(&laquo;Long option is : %s\n&raquo;,long_option[option_index].name);
            break;
        }
        printf(&laquo;flag_value = %d\n&raquo;,flag_value);
    }
    return 0;
}</pre></p>

<pre><code>                                                      &lt;p&gt;
                                                        &amp;nbsp;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; 

                                                        &lt;p&gt;
                                                          运行结果：
                                                        &lt;/p&gt;

                                                        &lt;p&gt;
                                                          &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_1337527059HQkS.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=779d825e-bea2-4de1-9563-d18e6671053b&amp;n=26833883_1337527059HQkS.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
                                                        &lt;/p&gt;

                                                        &lt;p&gt;
                                                          注意：getopt_long和getopt_long_only的返回值
                                                        &lt;/p&gt;

                                                        &lt;p&gt;
                                                          &lt;a class=&quot;evt&quot; href=&quot;http://blog.chinaunix.net/attachment/201205/20/26833883_1337527087i1XX.png&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://mail.gsymy.com/attachment/downloadex?e=wordpress%40gsymy.com&amp;o=1&amp;et=normal&amp;m=2_0%3ADzzzzye7hPs%24----3XZGQex&amp;f=19c38a3c-d577-4086-8152-542a97093e78&amp;n=26833883_1337527087i1XX.png&amp;ext=png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
                                                        &lt;/p&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; 

                                                        &lt;p&gt;
                                                          原文链接:&lt;a href=&quot;http://blog.chinaunix.net/uid-26833883-id-3215592.html#&quot; target=&quot;_blank&quot;&gt;http://blog.chinaunix.net/uid-26833883-id-3215592.html#&lt;/a&gt;
                                                        &lt;/p&gt;
</code></pre>

</div>


  <footer class="container">
  <div class="container navigation no-print">
  <h2>Navigation</h2>
  
  

    
    <a class="prev" href="http://replace-this-with-your-hugo-site.com/2015/08/19/vserrornoprjfile.html" title="VS编译出错，工程文件不存在">
      Previous
    </a>
    

    
    <a class="next" href="http://replace-this-with-your-hugo-site.com/2015/08/23/tcpdumpnoroot.html" title="非root用户使用tcpdump">
      Next
    </a>
    

  


</div>

  <div class="container comments">
  <h2>Comments</h2>
  
<div id="disqus_thread"></div>
<script type="text/javascript">
  (function() {
    
    
    if (window.location.hostname == "localhost")
      return;

    var dsq = document.createElement('script'); dsq.async = true; dsq.type = 'text/javascript';
    dsq.src = '//your_disqus_shortname.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>


</div>

</footer>

</article>
      <footer id="main-footer" class="container main_footer">
  

  <div class="container nav foot no-print">
  

  <a class="toplink" href="#">back to top</a>

</div>

  <div class="container credits">
  
<div class="container footline">
  
  code with <i class='fa fa-heart'></i>


</div>


  
<div class="container copyright">
  
  &copy; 2016 gsymy.com.


</div>


</div>

</footer>

    </main>
    
<script type="text/javascript">
  (function() {
    
    
    if (window.location.hostname == "localhost")
      return;

    var dsq = document.createElement('script'); dsq.async = true; dsq.type = 'text/javascript';
    dsq.src = '//your_disqus_shortname.disqus.com/count.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>



<script src="http://replace-this-with-your-hugo-site.com/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>



<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'Your Google Analytics tracking code', 'auto');
  ga('send', 'pageview');

</script>


    
  </body>
</html>

